#!/bin/bash
#install
source ./var
source ./Tmpdir

#install
Install_nginx(){
    #预安装nginx的版本号
    Version="1.18.0"
    Nginx_prefix="/usr/local/nginx"
    
    #调用Tmpdir函数
    Tmpdir
#    mkdir  "onetimeused-$(date +%s)"  && cd onetimeused-* && dir=`pwd`
    cd $dir && wget -V &> /dev/null || yum -y install wget

    #检查目标位置是否存在nginx目录
    nginx -V  &> /dev/null && echoRed "检测到已安装ngixn命令，$(which nginx)，故而退出" && rm -rf $dir && exit 8
    [ -d ${Nginx_prefix} ] && echoRed "检测到${Nginx_prefix}下已安装ngixn，故而退出" && rm -rf $dir && exit 1
    which nginx &> /dev/null && echoRed "检测到`which nginx`下已安装ngixn，故而退出" && rm -rf $dir && exit 6
    sleep 6
    echoYellow "检测到系统指定位置没有安装nginx服务，即可进行安装……"

    #创建nginx用户
    if id nginx &> /dev/null ;then
	 echoRed "nginx用户已存在，请仔细检查,是否已经安装nginx"
    else
        groupadd -r nginx && useradd -r -g nginx -s /sbin/nologin -M -c "create-time is `date +%Y/%m/%d` ;is web service" nginx
	if [ $? = 0 ];then
            echoGreen "nginx用户创建成功"
	else
	    echoRed "nginx用户创建失败，请检查是否锁定了相关文件，即将退出安装"
	    rm -rf $dir
	    exit 68
	fi
    fi

    echoYellow "start install $Version"

    sleep 6
    #安装依赖包
    yum install gcc gcc-c++ pcre pcre pcre-devel zlib zlib-devel openssl openssl-devel make -y
    yum install gcc gcc-c++ autoconf automake -y
    echoGreen "相关依赖包已完成安装"
    sleep 6

    #下载nginx安装包，并解压
    [ ! -f $dir/nginx-${Version}.tar.gz ] && echoRed "不存在相应的安装包,立即下载此安装包" && wget $ip/nginx-${Version}.tar.gz && tar -xzf nginx-${Version}.tar.gz && echoGreen "下载、解压完成…………"
    #[ -f $dir/nginx-${Version}.tar.gz ] && echoRed "存在相应的安装包" ||  wget $ip/nginx-${Version}.tar.gz
    \cp $dir/nginx-${Version}.tar.gz /usr/local/
    sleep 6
    
    #隐藏版本信息
    sed -i "s@${Version}\"@2\.4\.6\"@" $dir/nginx-${Version}/src/core/nginx.h
    sed -i "s|nginx\/|apache\/|" $dir/nginx-${Version}/src/core/nginx.h
    cat $dir/nginx-${Version}/src/core/nginx.h

    echoGreen "请检查隐藏版本信息是否正确，否则编译时会报错"
    sleep 6

    #编译与安装
    pwd
    cd nginx-${Version} && ./configure --prefix=${Nginx_prefix} --user=nginx --group=nginx --sbin-path=${Nginx_prefix}/sbin/nginx --conf-path=${Nginx_prefix}/conf/nginx.conf --http-log-path=${Nginx_prefix}/logs/access.log --error-log-path=${Nginx_prefix}/logs/error.log --pid-path=${Nginx_prefix}/logs/nginx.pid --lock-path=${Nginx_prefix}/logs/nginx.lock --with-http_flv_module --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_v2_module --with-http_realip_module --with-http_sub_module --with-pcre --with-stream --http-client-body-temp-path=${Nginx_prefix}/client_body_temp --http-proxy-temp-path=${Nginx_prefix}/proxy_temp --http-fastcgi-temp-path=${Nginx_prefix}/fastcgi_temp --http-uwsgi-temp-path=${Nginx_prefix}/uwsgi_temp --http-scgi-temp-path=${Nginx_prefix}/scgi_temp && make && make install
    if [ $? = 0 ] ;then
	 echoGreen "编译安装完成" 
         sleep 6
    else
	 rm -rf $dir
	 echoRed "编译安装失败，请检查，即将退出" 
	 exit 12
    fi 
    sleep 6
    #配置环境变量
    ln -sv ${Nginx_prefix}/sbin/nginx /usr/local/sbin

    echoGreen "配置环境变量已完成"
    ls -l ${Nginx_prefix}/sbin/nginx
    sleep 6

    sleep 6


    #配置nginx.conf
    [ -d ${Nginx_prefix}/conf ] && mkdir -p ${Nginx_prefix}/conf/vhost/
    if [ $? = 0 ];then
    #如果能下载nginx.conf，那么就备份已存在的nginx.conf，最后下载新的nginx.conf
        curl -I "https://download.gobgm.com/file/All_conf/nginx.conf" | grep "200" && mv ${Nginx_prefix}/conf/nginx.conf ${Nginx_prefix}/conf/nginx.conf.bak && wget -O ${Nginx_prefix}/conf/nginx.conf https://download.gobgm.com/file/All_conf/nginx.conf
    else 
        rm -rf $dir
	exit
        sleep 6
    fi

    #检查nginx是否正常
#    /usr/local/nginx/sbin/nginx -V &> /dev/null && echoGreen "已完成安装，可尽情享用" || echoYellow "可能安装有问题，请检查" 
    ${Nginx_prefix}/sbin/nginx -V &> /dev/null
    if [ $? = 0 ];then
	echoGreen "完成安装"
        echoGreen "nginx安装目录${Nginx_prefix}，已完成安装，可尽情享用"
    else
        sleep 6
       	rm -rf $dir
	exit 9
    fi
    #配置日志切割
    cat='cat'
    if [ ! -f /etc/logrotate.d/nginx ];then
cat > /etc/logrotate.d/nginx << EOF
${Nginx_prefix}/logs/*.log {
daily                
missingok            
rotate 7
dateext
dateformat -%Y-%m-%d-%s
compress           
delaycompress 
notifempty           
create 640 root root
sharedscripts        
postrotate
[ -f ${Nginx_prefix}/logs/nginx.pid ] && kill -USR1 \`${cat} ${Nginx_prefix}/logs/nginx.pid\`
endscript
}
EOF
        echoGreen "日志切割完成"
   else
	echoYellow "已存在nginx的日志切割脚本"
    fi

    #检查nginx的配置文件是否正确，正确后启动nginx
    ${Nginx_prefix}/sbin/nginx -V  && ${Nginx_prefix}/sbin/nginx -t &&  ${Nginx_prefix}/sbin/nginx 

    echoYellow "已成功启动nginx"
    #配置开机启动
    echo '/usr/local/nginx/sbin/nginx' >> /etc/rc.local && chmod +x /etc/rc.d/rc.local
    #删除安装nginx的临时目录
    rm -rf $dir
}
#Install_nginx
